package liantiaoxifen.client;

import liaotiangaizao.Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public  class Service {
    private BufferedReader br;
    private PrintWriter pw;
    private Scanner scan=new Scanner(System.in);
    private Dao dao=new Dao();

    public Service(BufferedReader br, PrintWriter pw) {
        this.br = br;
        this.pw = pw;
    }

    //处理登录功能
    public void loginhander(){
        int i=1;
        while (i<=5) {
            dao.sendDataLogin(pw);
            try {
                String key=null;
                if ("true".equals(key=br.readLine())){
                    System.out.println("恭喜你登录成功！");
                    Thread t=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String message=null;
                            try {
                                while ((message=br.readLine())!=null){
                                    System.out.println(message);
                                }
                            } catch (IOException e) {
                                System.out.println("");
                                e.printStackTrace();
                            }
                        }
                    });
                    t.setDaemon(true);
                    t.start();

                    System.out.println("接下来请输入消息");
                    String message;
                    while (true){
                        message=scan.nextLine();

                        if ("exit".equals(message)){
                            break;
                        }
                        pw.println(message);
                    }
                }else{
                    if (i==5){
                        System.out.println("用户名或密码错误次数太多！已退出程序，请稍后再试！");
                        break;
                    }
                    if ("falsef".equals(key)){
                        System.out.println("该用户已登录，请不要重复登录！");
                        return;
                    }
                    System.out.println("用户名或密码错误！请重试！"+i+"次");
                    i++;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
    //处理注册功能
    public int registerhander(){
        dao.sendDataRegister(pw);
        try {
            if ("true".equals(br.readLine())){
                System.out.println("注册成功！");
                return 1;
            }else {
                System.out.println("用户名已存在！注册失败！");
                System.out.println("是否想要继续注册？ 1.是 2.否");
                String i=scan.nextLine();
                pw.println(i);
                if ("1".equals(i))registerhander();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return 2;
    }

    public static void main(String[] args) {
        Client client=new Client();
        client.start();

    }
}
